```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>进程与线程深度解析 | Java多线程编程指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', 'Noto Serif SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background-color: #f8fafc;
            color: #1e293b;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
        }
        .code-block {
            background-color: #1e293b;
            border-left: 4px solid #8b5cf6;
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .list-item {
            position: relative;
            padding-left: 1.5rem;
        }
        .list-item:before {
            content: "";
            position: absolute;
            left: 0;
            top: 0.75rem;
            width: 0.5rem;
            height: 0.5rem;
            border-radius: 50%;
            background-color: #8b5cf6;
        }
        .thread-status {
            background: linear-gradient(to right, #f8fafc, #e2e8f0);
        }
        .highlight {
            position: relative;
        }
        .highlight:after {
            content: "";
            position: absolute;
            bottom: 0.1em;
            left: 0;
            width: 100%;
            height: 0.4em;
            background-color: #a5b4fc;
            opacity: 0.3;
            z-index: -1;
            border-radius: 2px;
        }
    </style>
</head>
<body class="min-h-screen">
    <!-- Hero Section -->
    <section class="hero min-h-96 text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl flex flex-col items-center justify-center">
            <div class="flex items-center mb-6">
                <i class="fas fa-microchip text-4xl mr-4 text-indigo-200"></i>
                <h1 class="text-4xl md:text-5xl font-bold tracking-tight">进程与线程<span class="highlight">深度解析</span></h1>
            </div>
            <p class="text-xl md:text-2xl text-indigo-100 max-w-3xl text-center leading-relaxed">
                探索Java多线程编程的核心概念与最佳实践
            </p>
            <div class="mt-10 flex space-x-4">
                <div class="bg-white bg-opacity-20 px-6 py-3 rounded-full flex items-center">
                    <i class="fas fa-code-branch mr-2"></i>
                    <span>并发编程</span>
                </div>
                <div class="bg-white bg-opacity-20 px-6 py-3 rounded-full flex items-center">
                    <i class="fas fa-project-diagram mr-2"></i>
                    <span>JVM内存模型</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-16">
        <!-- Process & Thread Definition -->
        <section class="mb-20">
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-indigo-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-box-open text-indigo-600 text-xl"></i>
                            </div>
                            <h2 class="text-2xl font-bold text-gray-800">进程</h2>
                        </div>
                        <p class="text-gray-700">
                            在内存中运行的应用程序。每个进程都有自己独立的一块内存空间，一个进程就是一个xxx.exe。
                        </p>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-bolt text-purple-600 text-xl"></i>
                            </div>
                            <h2 class="text-2xl font-bold text-gray-800">线程</h2>
                        </div>
                        <p class="text-gray-700">
                            进程中的一个执行任务（控制单元），负责当前进程中程序的执行，一个进程可以运行多个线程，多个线程可共享数据。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Differences Section -->
        <section class="mb-20">
            <div class="flex items-center mb-8">
                <div class="h-px bg-gray-300 flex-1"></div>
                <h2 class="text-3xl font-bold px-4 text-center">进程与线程的<span class="highlight">核心区别</span></h2>
                <div class="h-px bg-gray-300 flex-1"></div>
            </div>

            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-sitemap mr-2"></i>根本区别
                    </h3>
                    <p class="text-gray-700">
                        进程是操作系统资源分配的基本单位，而线程是处理器任务调度和执行的基本单位
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-purple-600 flex items-center">
                        <i class="fas fa-tachometer-alt mr-2"></i>资源开销
                    </h3>
                    <p class="text-gray-700">
                        每个进程都有独立的代码和数据空间，程序之间的切换会有较大的开销；每个线程都有自己独立的运行栈和程序计数器（PC），线程之间切换的开销小。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600 flex items-center">
                        <i class="fas fa-layer-group mr-2"></i>包含关系
                    </h3>
                    <p class="text-gray-700">
                        如果一个进程内有多个线程，则执行过程不是一条线的，而是多条线（线程）共同完成的；线程是进程的一部分。
                    </p>
                </div>
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h3 class="text-xl font-semibold mb-4 text-purple-600 flex items-center">
                        <i class="fas fa-memory mr-2"></i>内存分配
                    </h3>
                    <p class="text-gray-700">
                        同一进程的线程共享本进程的地址空间和资源，而进程之间的地址空间和资源是相互独立的
                    </p>
                </div>
            </div>
        </section>

        <!-- Concurrency vs Parallelism -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">并发与<span class="highlight">并行</span></h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-orange-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-random text-orange-600 text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold text-gray-800">并发 (Concurrency)</h3>
                        </div>
                        <p class="text-gray-700">
                            单核CPU，多个线程操作同一个资源，在一个CPU上跑多个线程，通过快速交替的方式，达到一种并行的假象。
                        </p>
                    </div>
                </div>
                <div class="bg-white rounded-xl shadow-md overflow-hidden card">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 p-3 rounded-lg mr-4">
                                <i class="fas fa-parallel text-green-600 text-xl"></i>
                            </div>
                            <h3 class="text-2xl font-bold text-gray-800">并行 (Parallelism)</h3>
                        </div>
                        <p class="text-gray-700">
                            多核CPU，多个线程同时执行，在每个CPU上同时都有一个线程在运行。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Thread Initialization Methods -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">线程的四种<span class="highlight">初始化方式</span></h2>
            
            <!-- Method 1 -->
            <div class="mb-12">
                <div class="flex items-center mb-6">
                    <div class="bg-indigo-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4 font-bold">1</div>
                    <h3 class="text-2xl font-bold text-gray-800">继承Thread类</h3>
                </div>
                <div class="bg-gray-50 p-6 rounded-lg mb-6">
                    <p class="text-gray-700 mb-4">
                        继承Thread类复写父类中的run()。Thread类也是Runnable接口的一个子类。要想启动线程必须依靠Thread类的start()方法执行，线程启动之后会默认调用run()方法,一个线程只能启动唯一一次，多次启动会抛异常。
                    </p>
                    <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code class="language-java">public class ThreadDemo {
    public static void main(String[] args) {
        Thread myThread1 = new MyThread();     // 创建一个新的线程  myThread1  此线程进入新建状态
        Thread myThread2 = new MyThread();     // 创建一个新的线程 myThread2 此线程进入新建状态
        myThread1.start();                     // 调用start()方法使得线程进入就绪状态
        myThread2.start();                     // 调用start()方法使得线程进入就绪状态
    }
}

class MyThread extends Thread {
    private int i = 0;
    @Override
    public void run() {
        for (i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <!-- Method 2 -->
            <div class="mb-12">
                <div class="flex items-center mb-6">
                    <div class="bg-purple-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4 font-bold">2</div>
                    <h3 class="text-2xl font-bold text-gray-800">实现Runnable接口</h3>
                </div>
                <div class="bg-gray-50 p-6 rounded-lg mb-6">
                    <p class="text-gray-700 mb-4">
                        因为Java是单继承的，一个类只能有一个父类，所以如果已经继承了一个类就不能在继承Thread类了。所以这时候启动线程可以用实现Runnable接口来完成。
                    </p>
                    <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code class="language-java">public class ThreadDemo {
    public static void main(String[] args) {
        Runnable myRunnable = new MyRunnable(); 
        new Thread(myRunnable).start();
        new Thread(myRunnable).start(); 
    }
}

class MyRunnable implements Runnable {
    private int i = 0;

    @Override
    public void run() {
        for (i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <!-- Method 3 -->
            <div class="mb-12">
                <div class="flex items-center mb-6">
                    <div class="bg-blue-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4 font-bold">3</div>
                    <h3 class="text-2xl font-bold text-gray-800">实现Callable接口</h3>
                </div>
                <div class="bg-gray-50 p-6 rounded-lg mb-6">
                    <p class="text-gray-700 mb-4">
                        实现Callable接口相对于实现Runnable接口它有<strong>返回值</strong>，而且还可以抛出异常，执行 Callable 方式，需要 FutureTask 实现类的支持，用于接收运算结果。
                    </p>
                    <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code class="language-java">public class ThreadDemo {
    public static void main(String[] args) {
        // 创建MyCallable对象
        Callable<Integer> myCallable = new MyCallable();

        //使用FutureTask来包装MyCallable对象
        FutureTask<Integer> ft = new FutureTask<Integer>(myCallable);

        //FutureTask对象作为Thread对象的target创建新的线程
        new Thread(ft).start();

        System.out.println("主线程for循环执行完毕..");

        try {
            //取得新创建的新线程中的call()方法返回的结果
            // get方法会阻塞，一直等待call()方法执行完后才会执行。
            int sum = ft.get();
            System.out.println("sum = " + sum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class MyCallable implements Callable<Integer> {
    private int i = 0;
    @Override
    public Integer call() {
        int sum = 0;
        for (; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
            sum += i;
        }
        return sum;
    }
}</code></pre>
                    </div>
                </div>
            </div>
            
            <!-- Method 4 -->
            <div class="mb-12">
                <div class="flex items-center mb-6">
                    <div class="bg-green-600 text-white rounded-full w-10 h-10 flex items-center justify-center mr-4 font-bold">4</div>
                    <h3 class="text-2xl font-bold text-gray-800">线程池</h3>
                </div>
                <div class="bg-gray-50 p-6 rounded-lg mb-6">
                    <p class="text-gray-700 mb-4">
                        线程池是一种多线程处理形式，处理过程中将任务添加到队列，然后在创建线程后自动启动这些任务。线程池线程都是后台线程。
                    </p>
                    <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto">
                        <pre><code class="language-java">private static ThreadPoolExecutor executors = null;
    
static {
    executors = new ThreadPoolExecutor(12, // 核心线程池数量，初始化时线程池中的数量
            50, // 最大的线程池数量
            10, // 时间
            TimeUnit.SECONDS, // 时间单位
            new ArrayBlockingQueue<>(500), // 队列的长度
            Executors.defaultThreadFactory(), // 线程工厂
            new ThreadPoolExecutor.DiscardPolicy()); // 拒绝策略
}

public static ThreadPoolExecutor getPoll(){
    return executors;
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Thread States -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center">线程的<span class="highlight">生命周期</span>与状态转换</h2>
            
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8">
                <div class="p-8">
                    <div class="flex flex-col md:flex-row items-center">
                        <div class="w-full md:w-1/2 mb-6 md:mb-0 md:pr-8">
                            <h3 class="text-2xl font-bold text-gray-800 mb-4">线程状态图</h3>
                            <div class="mermaid">
                                stateDiagram-v2
                                [*] --> 新建状态
                                新建状态 --> 就绪状态: start()
                                就绪状态 --> 运行状态: 获得CPU时间片
                                运行状态 --> 阻塞状态: 等待I/O、sleep()、wait()等
                                阻塞状态 --> 就绪状态: I/O完成、sleep结束、notify()
                                运行状态 --> 就绪状态: 时间片用完或yield()
                                运行状态 --> 死亡状态: 执行完成或异常终止
                            </div>
                        </div>
                        <div class="w-full md:w-1/2">
                            <h3 class="text-2xl font-bold text-gray-800 mb-4">详细状态说明</h3>
                            <ul class="space-y-4">
                                <li class="list-item">
                                    <strong>新建状态 (New)</strong>: 当线程对象对创建后，即进入了新建状态，如：Thread t = new MyThread();
                                </li>
                                <li class="list-item">
                                    <strong>就绪状态 (Runnable)</strong>: 当调用线程对象的start()方法，线程即进入就绪状态。处于就绪状态的线程，只是说明此线程已经做好了准备，随时等待CPU调度执行。
                                </li>
                                <li class="list-item">
                                    <strong>运行状态 (Running)</strong>: 当CPU开始调度处于就绪状态的线程时，此时线程才得以真正执行，即进入到运行状态。
                                </li>
                                <li class="list-item">
                                    <strong>阻塞状态 (Blocked)</strong>: 处于运行状态中的线程由于某种原因，暂时放弃对CPU的使用权，停止执行，此时进入阻塞状态。
                                </li>
                                <li class="list-item">
                                    <strong>死亡状态 (Dead)</strong>: 线程执行完了或者因异常退出了run()方法，该线程结束生命周期。
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Sleep vs Wait -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="p-8">
                    <h3 class="text-2xl font-bold text-gray-800 mb-6">Sleep与Wait的<span class="highlight">锁释放区别</span></h3>
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto mb-4">
                                <pre><code class="language-java">public class SleepDemo {
    public static void main(String[] args) {
        MyRunable myRunable = new MyRunable();
        for (int i = 0; i < 5; i++) {
            new Thread(myRunable, "线程-" + i).start();
        }
    }
}

class MyRunable implements Runnable {
    @Override
    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + " 进来了。。。开始休眠");
            try {
                Thread.sleep(2000);  // sleep不释放锁
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 休眠结束");
        }
    }
}</code></pre>
                            </div>
                        </div>
                        <div>
                            <div class="code-block text-gray-200 p-4 rounded-lg overflow-x-auto mb-4">
                                <pre><code class="language-java">public class SleepDemo {
    public static void main(String[] args) {
        MyRunable myRunable = new MyRunable();
        for (int i = 0; i < 5; i++) {
            new Thread(myRunable, "线程-" + i).start();
        }
    }
}

class MyRunable implements Runnable {
    @Override
    public void run() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + " 进来了。。。开始等待");
            try {
                this.wait();  // wait会释放锁
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " 等待结束");
        }
    }
}</code></pre>
                            </div>
                        </div>
                    </div>
                    <div class="mt-6 p-4 bg-blue-50 rounded-lg">
                        <h4 class="font-bold text-blue-700 mb-2 flex items-center">
                            <i class="fas fa-info-circle mr-2"></i>关键区别
                        </h4>
                        <ul class="text-blue-700 space-y-2">
                            <li class="list-item">sleep()是Thread类的方法，wait()是Object类的方法</li>
                            <li class="list-item">sleep()不会释放锁，wait()会释放锁</li>
                            <li class="list-item">sleep()必须指定时间，wait()可以不指定时间(需notify/notifyAll唤醒)</li>
                            <li class="list-item">sleep()可以在任何地方使用，wait()必须在同步代码块中使用</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```